//
//  AXFIndentStatusController.m
//  BeeQuick
//
//  Created by 王誉升 on 2016/11/21.
//  Copyright © 2016年 six_group. All rights reserved.
//

#import "AXFIndentStatusController.h"
#import "AXFIndentStatusCell.h"
#import "AXFIndentStatusModel.h"

static NSString *StatusCell = @"StatusCell_ID";
@interface AXFIndentStatusController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,weak) UITableView *tbView;

@property (nonatomic,weak) UIView *blView;

@end

@implementation AXFIndentStatusController
{
    NSArray <AXFIndentStatusModel *> *_IndentModelList;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

- (void)setupUI
{
    self.automaticallyAdjustsScrollViewInsets = YES;
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height ) style:UITableViewStylePlain];
    
    [self loadData];
    
    tableView.delegate = self;
    tableView.dataSource = self;
    
    tableView.rowHeight = 80;
    
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [tableView registerNib:[UINib nibWithNibName:@"AXFIndentStatusCell" bundle:nil] forCellReuseIdentifier:StatusCell];
    
    [self.view addSubview:tableView];
    
    self.tbView = tableView;
    
    UIView *belowView = [[UIView alloc] init];
    
    belowView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:belowView];
    
    [belowView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.bottom.equalTo(self.tbView);
        
        make.height.mas_offset(44);
        
    }];
    
    self.blView = belowView;
    
    UIView *bottomSplitLineV = [[UIView alloc] init];
    
    bottomSplitLineV.backgroundColor = [UIColor lightGrayColor];
    
    [belowView addSubview:bottomSplitLineV];
    
    [bottomSplitLineV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.equalTo(self.blView);
        
        make.height.mas_offset(1);
        
    }];
    
    UIButton *deleteBt = [UIButton buttonWithType:UIButtonTypeCustom];

    [deleteBt setTitle:@"删除订单" forState:UIControlStateNormal];
    
    [deleteBt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    deleteBt.backgroundColor = [UIColor yellowColor];

    deleteBt.titleLabel.font = [UIFont systemFontOfSize:13];
    
//    deleteBt.layer.cornerRadius = 3.0;
//    
//    deleteBt.layer.masksToBounds = YES;
    
    [deleteBt addTarget:self action:@selector(clickToDeletBt:) forControlEvents:UIControlEventTouchUpInside];
    
    [belowView addSubview:deleteBt];
    
    [deleteBt mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.blView);
        
        make.right.equalTo(self.blView).offset(-20);
        
    }];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"投诉" style:UIBarButtonItemStylePlain target:self action:@selector(clickToRightItem:)];
    
    self.navigationItem.rightBarButtonItem = rightItem;
    
    UISegmentedControl *segmentC = [[UISegmentedControl alloc]initWithItems:@[@"订单状态",@"订单详情"]];
    
    segmentC.tintColor = [UIColor yellowColor];
    
    //设置宽度
    [segmentC setWidth:80 forSegmentAtIndex:0];
    [segmentC setWidth:80 forSegmentAtIndex:1];
    
    //设置默认选中
    [segmentC setSelectedSegmentIndex:0];
    
    NSDictionary *selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:12],NSForegroundColorAttributeName: [UIColor blackColor]};
    
    [segmentC setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];
    
    NSDictionary *unSelectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:12],NSForegroundColorAttributeName: [UIColor grayColor]};
    
    [segmentC setTitleTextAttributes:unSelectedTextAttributes forState:UIControlStateNormal];
    
    //监听事件
    [segmentC addTarget:self action:@selector(clickSegmentC:) forControlEvents:UIControlEventValueChanged];
    
    self.navigationItem.titleView = segmentC;
    
    //自定义导航条返回按钮
    UIBarButtonItem *leftBarB = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"v2_goback"] style:UIBarButtonItemStylePlain target:self action:@selector(clickToVC:)];
    
    self.navigationItem.leftBarButtonItem = leftBarB;
}

- (void)clickToVC:(UIBarButtonItem *)batBtn
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)clickToDeletBt:(UIButton *)sender
{
    NSLog(@"删除订单");
}

- (void)clickToRightItem:(UIBarButtonItem *)sender
{
    NSLog(@"投诉");
}

- (void)clickSegmentC:(UISegmentedControl *)sender {
    switch (sender.selectedSegmentIndex) {
            case 0:
            NSLog(@"---");
            break;
            
            case 1:
            NSLog(@"+++");
            break;
            
        default:
            break;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _IndentModelList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AXFIndentStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:StatusCell forIndexPath:indexPath];
    
    AXFIndentStatusModel *model = _IndentModelList[indexPath.row];
    if (indexPath.row == 0)
    {
        model.firstStatus = YES;
    }
    else if(indexPath.row == _IndentModelList.count - 1)
    {
        model.lastStatus = YES;
    }
    cell.model = model;
    
    return cell;
}

- (void)loadData
{
    NSURL *url = [[NSBundle mainBundle]URLForResource:@"IndentStatusList.plist" withExtension:nil];
    
    NSArray *array = [NSArray arrayWithContentsOfURL:url];
    
    NSMutableArray <AXFIndentStatusModel *> *arrayM = [NSMutableArray array];
    
    for (NSDictionary *dict in array) {
        AXFIndentStatusModel *model = [AXFIndentStatusModel indentstatusWithDict:dict];
        
        [arrayM addObject:model];
    }
    
    _IndentModelList = arrayM.copy;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
